package com.cxlin.cwanandroid.ui.home.adapter

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.databinding.library.baseAdapters.BR
import com.cxlin.base.ext.startActivity
import com.cxlin.base.view.recyclerview.adapter.BaseAdapter
import com.cxlin.cwanandroid.R
import com.cxlin.cwanandroid.databinding.ItemNavigationBinding
import com.cxlin.cwanandroid.model.bean.Article
import com.cxlin.cwanandroid.model.bean.Navigation
import com.cxlin.cwanandroid.ui.browser.BrowserActivity
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/5/6 10:42
 */
class NavigationAdapter() : BaseAdapter<Navigation,ItemNavigationBinding>(R.layout.item_navigation,
    BR.item){

    override fun onBindViewHolder(
        holder: VH<ItemNavigationBinding>,
        position: Int,
        item: Navigation?
    ) {
        super.onBindViewHolder(holder, position, item)
        item?.run {
            holder.binding.navigationTagLayout.adapter = object : TagAdapter<Article>(item.articles){

                override fun getCount(): Int {
                    return item.articles.size
                }

                override fun getView(parent: FlowLayout, position: Int, t: Article): View {
                    val tv = LayoutInflater.from(parent.context).inflate(R.layout.item_tag,parent,false) as TextView
                    tv.text = t.title
                    tv.setOnClickListener {

                    }
                    return tv
                }

            }
            holder.binding.navigationTagLayout.setOnTagClickListener { view, position, parent ->
                parent.context.startActivity<BrowserActivity>(data = Bundle().apply {
                    putString("url",articles[position].link)
                })
                true
            }
        }
    }
}